import 'package:flutter/material.dart';

class MapPickerView extends StatefulWidget {
  const MapPickerView({super.key});

  @override
  State<MapPickerView> createState() => _MapPickerViewState();
}

class _MapPickerViewState extends State<MapPickerView> {
  // 模拟地图位置
  double _latitude = 39.9042;
  double _longitude = 116.4074;
  String _selectedAddress = "北京市东城区天安门";
  
  @override
  void initState() {
    super.initState();
    print("地图选择页面初始化");
  }
  
  // 模拟点击地图上不同位置
  void _selectLocation(int locationIndex) {
    setState(() {
      switch(locationIndex) {
        case 1:
          _latitude = 39.9042;
          _longitude = 116.4074;
          _selectedAddress = "北京市东城区天安门";
          break;
        case 2:
          _latitude = 31.2304;
          _longitude = 121.4737;
          _selectedAddress = "上海市黄浦区人民广场";
          break;
        case 3:
          _latitude = 22.5431;
          _longitude = 114.0579;
          _selectedAddress = "深圳市福田区市民中心";
          break;
        case 4:
          _latitude = 30.6568;
          _longitude = 104.0654;
          _selectedAddress = "成都市锦江区春熙路";
          break;
      }
    });
    print("选择了位置: $_selectedAddress (${_latitude.toStringAsFixed(4)}, ${_longitude.toStringAsFixed(4)})");
  }
  
  // 确认选择位置并返回
  void _confirmLocation() {
    print("确认选择位置: $_selectedAddress (${_latitude.toStringAsFixed(4)}, ${_longitude.toStringAsFixed(4)})");
    Navigator.pop(context, {
      'latitude': _latitude,
      'longitude': _longitude,
      'address': _selectedAddress
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('选择位置', style: TextStyle(color: Colors.white)),
        backgroundColor: Color(0xff00AEEE),
        iconTheme: IconThemeData(color: Colors.white),
      ),
      backgroundColor: Color(0xff00AEEE),
      body: Column(
        children: [
          // 提示信息
          Container(
            margin: EdgeInsets.all(15),
            padding: EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Row(
              children: [
                Icon(Icons.info, color: Colors.orange),
                SizedBox(width: 10),
                Expanded(
                  child: Text(
                    "请点击下方预设位置或地图区域选择您的收货地址",
                    style: TextStyle(
                      color: Colors.black87,
                    ),
                  ),
                ),
              ],
            ),
          ),
          
          // 模拟地图区域
          Expanded(
            child: Container(
              margin: EdgeInsets.all(15),
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(0.9),
                borderRadius: BorderRadius.circular(10),
              ),
              child: Stack(
                children: [
                  // 地图背景
                  Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          Icons.map,
                          size: 100,
                          color: Color(0xff00AEEE).withOpacity(0.2),
                        ),
                      ],
                    ),
                  ),
                  
                  // 位置标记
                  Positioned(
                    top: 80,
                    left: 100,
                    child: GestureDetector(
                      onTap: () => _selectLocation(1),
                      child: Icon(
                        Icons.location_pin,
                        color: _selectedAddress == "北京市东城区天安门" ? Colors.red : Colors.grey,
                        size: 30,
                      ),
                    ),
                  ),
                  
                  Positioned(
                    top: 150,
                    right: 80,
                    child: GestureDetector(
                      onTap: () => _selectLocation(2),
                      child: Icon(
                        Icons.location_pin,
                        color: _selectedAddress == "上海市黄浦区人民广场" ? Colors.red : Colors.grey,
                        size: 30,
                      ),
                    ),
                  ),
                  
                  Positioned(
                    bottom: 100,
                    right: 100,
                    child: GestureDetector(
                      onTap: () => _selectLocation(3),
                      child: Icon(
                        Icons.location_pin,
                        color: _selectedAddress == "深圳市福田区市民中心" ? Colors.red : Colors.grey,
                        size: 30,
                      ),
                    ),
                  ),
                  
                  Positioned(
                    bottom: 80,
                    left: 80,
                    child: GestureDetector(
                      onTap: () => _selectLocation(4),
                      child: Icon(
                        Icons.location_pin,
                        color: _selectedAddress == "成都市锦江区春熙路" ? Colors.red : Colors.grey,
                        size: 30,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          
          // 显示选择的位置
          Container(
            margin: EdgeInsets.symmetric(horizontal: 15),
            padding: EdgeInsets.all(15),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              children: [
                Row(
                  children: [
                    Icon(Icons.location_on, color: Colors.red),
                    SizedBox(width: 10),
                    Expanded(
                      child: Text(
                        _selectedAddress,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 5),
                Row(
                  children: [
                    Icon(Icons.gps_fixed, color: Colors.blue, size: 16),
                    SizedBox(width: 10),
                    Text(
                      "坐标: (${_latitude.toStringAsFixed(4)}, ${_longitude.toStringAsFixed(4)})",
                      style: TextStyle(
                        fontSize: 12,
                        color: Colors.grey[700],
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          
          // 确认按钮
          Padding(
            padding: const EdgeInsets.all(20),
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.white,
                foregroundColor: Color(0xff00AEEE),
                padding: EdgeInsets.symmetric(vertical: 15),
                minimumSize: Size(double.infinity, 50),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(25),
                ),
              ),
              onPressed: _confirmLocation,
              child: Text('确认选择此位置', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
            ),
          ),
        ],
      ),
    );
  }
} 